{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.append()`\n",
    "\n",
    "- 向列表的最后添加一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(f'原列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_city.append('Beijing')\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.insert()`\n",
    "\n",
    "- 向列表的指定位置插入一个元素\n",
    "- 参数：\n",
    "    1. 要插入的位置\n",
    "    2. 要插入的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(f'原列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_city.insert(2,'Beijing')\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_city[6:] = ['Shang Hai']\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_city.insert(7,'Xi An')\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.extend()`\n",
    "\n",
    "- 使用新的序列扩展当前序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(f'原列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_city.extend(['Beijing','Shang Hai']) # my_city += ['Beijing','Shang Hai']\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.clear()`\n",
    "\n",
    "- 清空序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(f'原列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_city.clear()\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.pop()`\n",
    "\n",
    "- 根据索引删除并返回指定元素（默认是删除最后一个元素）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(f'原列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "third_city = my_city.pop(2)\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')\n",
    "print(f'third_city = {third_city}')\n",
    "\n",
    "last_city = my_city.pop()\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')\n",
    "print(f'last_city = {last_city}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.remove()`\n",
    "\n",
    "- 删除指定值的元素\n",
    "- 如果相同值的元素有多个，只会删除第一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(f'原列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_city.remove('Tokyo')\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.reverse()`\n",
    "\n",
    "- 反转列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(f'原列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_city.reverse()\n",
    "print(f'修改后列表 = {my_city}, id = {id(my_city)}')\n",
    "\n",
    "my_name = list('icekylin')\n",
    "print(f'原列表 = {my_name}, id = {id(my_name)}')\n",
    "\n",
    "my_name.reverse()\n",
    "print(f'修改后列表 = {my_name}, id = {id(my_name)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `.sort()`\n",
    "\n",
    "- 用来对列表中的元素进行排序\n",
    "- 默认升序排列，如果需要降序排列需要添加参数 `reverse=True`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_letter = list('qwertyuiopasdfghjklzxcvbnm')\n",
    "print(f'原列表 = {my_letter}, id = {id(my_letter)}')\n",
    "\n",
    "my_letter.sort()\n",
    "print(f'修改后列表 = {my_letter}, id = {id(my_letter)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_letter = list('qwertyuiopasdfghjklzxcvbnm')\n",
    "print(f'原列表 = {my_letter}, id = {id(my_letter)}')\n",
    "\n",
    "my_letter.sort(reverse=True)\n",
    "print(f'修改后列表 = {my_letter}, id = {id(my_letter)}')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.5 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}